'use strict';
import {ajax, nameSpace } from 'utils/index';

let ns = nameSpace('RingTagStep3');
export const SET_FIELD = ns('SET_FIELD');
export const GET_ITEMS_ON_SEAL = ns('GET_ITEMS_ON_SEAL');
export const GET_ITEMS_IN_DEPOT = ns('GET_ITEMS_IN_DEPOT');
export const GET_SELLER_CATS = ns('GET_SELLER_CATS');
export const SHOW_LOADING = ns('SHOW_LOADING');
export const CLOSE_DIALOG = ns('CLOSE_DIALOG');
export const LOAD_ERR = ns('LOAD_ERR');
export const SET_ITEMS = ns('SET_ITEMS');
export const ADD_ITEM_TO_ACTIVE = ns('ADD_ITEM_TO_ACTIVE');
export const LOAD_SET_ITEMS_IN_DATABASE = ns('LOAD_SET_ITEMS_IN_DATABASE');
export function setField(name,value) {
  return {type: SET_FIELD,name: name,value:value};
}
export function closeDialog(name,value) {
  return {type: CLOSE_DIALOG,visible:false};
}
export function errDialog(msg) {
  return {type: LOAD_ERR,...msg};
}

export function resetLimit(item,value){
	item.is_limit_buy = true;
	item.limit_buy_count = value;
	item.value = item.value?item.value:0;
	return {type: SET_ITEMS,itemset:item};
}
export function resetLimitItem(ztype,item,value) {
	 return (dispatch) => {
		ajax({
		  api: "math",
		  method: 'GET',
		  data:{price:item.price,value}
		},(json) => {
			item.finaPrice = json.finaPrice;
			item.value = value;
			item.ztype = "discountRate";
			item.promotion_value = value;
			dispatch({
				type: SET_ITEMS,
				itemset:item
			});
		},(json) => {
			const login = (json.code==210);
			dispatch({
				type: LOAD_ERR,
				message:json.msg+(json.sub_msg?json.sub_msg:""),
				loginFalse:login
			});
		});
	 }
}
export function setItems(ztype,item,value) {
	if(ztype=="finalPrice"){
		ztype = "decreaseAmount";
		value = (item.price - value);
	}
	let finaPrice = item.price;
	switch(ztype){
		case "decreaseAmount":
			item.promotion_type=0;
			finaPrice = item.price - value;
		break;
		case "discountRate" :
			item.promotion_type=1;
			finaPrice = item.price * (value/10);
		break;
	}
	item.finaPrice = finaPrice;
	item.value = value;
	item.ztype = ztype;
	item.promotion_value = value;
  return {type: SET_ITEMS,itemset:item};
}
export function getItemsInRingActive(activity_id) {
	return (dispatch) => {
		//发送请求前
		dispatch({
		  type: SHOW_LOADING,
		  loading:true
		});
		ajax({
			  api: 'getItemsInRingActive',
			  method: 'GET',
			  data:{activity_id}
			},(json) => {
				if(!json.items){
					json.items = [];
				}
				dispatch({type: LOAD_SET_ITEMS_IN_DATABASE,itemset:json.items});
			},(json) => {
				const login = (json.code==210);
				dispatch({
					type: LOAD_ERR,
					message:json.msg+(json.sub_msg?json.sub_msg:""),
					loginFalse:login
				})
			});
				
			// var itemset = [];
			// const insession = sessionStorage.getItem("setItems");
			// if(insession){
				// let items = JSON.parse(action.set_items);
				// if(items instanceof Array){
					// items.map(item=>{
						// itemset[item.num_iid]=item;
					// });
				// }
			// }
	}
}
export function getSellerCats(params) {
		return (dispatch) => {
			//发送请求前
			dispatch({
			  type: SHOW_LOADING,
			  loading:true
			});
			if(!params.nick){
				dispatch({
					type: LOAD_ERR,
					message:"登陆信息丢失，请重新登陆",
					loginFalse:true
				});
				return;
			}
			if(window.workbench){
				//千牛环境执行创建折扣,含有workbench参数为千牛环境
				dispatch({
						type: LOAD_ERR,
						message:"抱歉，当前版本不支持千牛环境。请联系旺旺：kekydream 获取千牛版本。",
						loginFalse:false
					});
				return;
				  window.QN.top.invoke( {
						cmd : 'taobao.sellercats.list.get',
						param : params,
						method:"post",
						success : function(rsp, cmd, param) {
							console.log(rsp,param);
						},
						error : function(msg, cmd, param) {
							console.log(msg,param);
						}
					} );
			}else{
				ajax({
				  api: 'getSellerCats',
				  method: 'GET',
				  data:params
				},(json) => {
					if(json.seller_cats && !(json.seller_cats.seller_cat instanceof Array)){
						json.seller_cats.seller_cat = Array(json.seller_cats.seller_cat);
					}
					dispatch({
					  type: GET_SELLER_CATS,
					  seller_cats:json.seller_cats.seller_cat
					});
				},(json) => {
					const login = (json.code==210);
					dispatch({
						type: LOAD_ERR,
						message:json.msg+(json.sub_msg?json.sub_msg:""),
						loginFalse:login
					})
				});
			}
		}
}
export function addItemToActive(itemset,act_type,tag_id) {

	return (dispatch) => {
		//发送请求前
		dispatch({
		  type: SHOW_LOADING,
		  loading:true
		});
		if(window.workbench){
			//千牛环境执行创建折扣,含有workbench参数为千牛环境
				let cmd = "";
				let params = "";
				switch(act_type){
					case "limit":
						cmd = "taobao.ump.detail.add";
					break;
					case "item":
						cmd = "taobao.promotionmisc.common.item.detail.add";
						params = {
							activity_id : itemset.activity_id,
							item_id:itemset.num_iid,
							promotion_value:itemset.promotion_value*100,
							promotion_type:itemset.promotion_type
						}
					break;
				}
			  window.QN.top.invoke( {
					cmd : cmd,
					param : params,
					method:"post",
					success : function(rsp, cmd, param) {
						let setItems = sessionStorage.getItem("setItems");
						if(setItems){
							setItems = JSON.parse(setItems);
						}else{
							setItems = Array();
						}
						setItems.push({...itemset,status:"done",detail_id:json.detail_id});
						sessionStorage.setItem("setItems",JSON.stringify(setItems));
						dispatch({type: ADD_ITEM_TO_ACTIVE,newItemSet:itemset});
					},
					error : function(msg, cmd, param) {
						dispatch({
							type: LOAD_ERR,
							message:msg,
							loginFalse:false
						});
					}
				} );
		}else{
			
			let cmd = "";
			let params={};
			switch(act_type){
				case "limit":
					cmd = "addLimitToRingActive";
					params = {
						activity_id : itemset.activity_id,
						item_id:itemset.num_iid,
						promotion_value:itemset.promotion_value,
						promotion_type:itemset.promotion_type,
						is_limit_buy:true,
						limit_count:itemset.limit_buy_count,
						price:itemset.price
					}
				break;
				case "item":
					cmd = "addItemsToRingActive";
					params = {
						activity_id : itemset.activity_id,
						item_id:itemset.num_iid,
						promotion_value:itemset.promotion_value,
						promotion_type:itemset.promotion_type
					}
				break;
			}
			ajax({
			  api: cmd,
			  method: 'GET',
			  data:params
			},(json) => {
				const newItemSet = {...itemset,status:"done",detail_id:json.detail_id,act_type};
				ajax({
				  api: "saveRingItemToDatabase",
				  method: 'GET',
				  data:newItemSet
				},(data_json) => {
					dispatch({type: ADD_ITEM_TO_ACTIVE,newItemSet});
				},(data_json) =>{
					const login = (json.code==210);
					dispatch({
						type: LOAD_ERR,
						message:data_json.msg+(data_json.sub_msg?data_json.sub_msg:""),
						loginFalse:login
					});
				});
			},(json) =>{
				const login = (json.code==210);
				dispatch({
					type: LOAD_ERR,
					message:json.msg+(json.sub_msg?json.sub_msg:""),
					loginFalse:login
				});
			});
			
		}
	}
}
export function editItemInActive(itemset,act_type) {

	return (dispatch) => {
		//发送请求前
		dispatch({
		  type: SHOW_LOADING,
		  loading:true
		});
		if(window.workbench){
			//千牛环境执行创建折扣,含有workbench参数为千牛环境
			itemset.promotion_value=itemset.promotion_value*100;
			  window.QN.top.invoke( {
					cmd : 'taobao.promotionmisc.common.item.detail.update',
					param : params,
					method:"post",
					success : function(rsp, cmd, param) {
						let setItems = sessionStorage.getItem("setItems");
							setItems = JSON.parse(setItems);
						sessionStorage.setItem("setItems",JSON.stringify(setItems));
						dispatch({type: ADD_ITEM_TO_ACTIVE,newItemSet:itemset});
					},
					error : function(msg, cmd, param) {
						dispatch({
							type: LOAD_ERR,
							message:msg,
							loginFalse:false
						});
					}
				} );
		}else{
			let cmd = "";
			let params={};
			switch(act_type){
				case "limit":
					cmd = "editLimitInRingActive";
					params = {
						activity_id : itemset.activity_id,
						item_id:itemset.num_iid,
						promotion_value:itemset.promotion_value,
						promotion_type:itemset.promotion_type,
						is_limit_buy:true,
						limit_count:itemset.limit_buy_count,
						price:itemset.price,
						detail_id:itemset.detail_id
					}
				break;
				case "item":
					cmd = "editItemInRingActive";
					 params = {
						activity_id : itemset.activity_id,
						item_id:itemset.num_iid,
						promotion_value:itemset.promotion_value,
						promotion_type:itemset.promotion_type,
						detail_id:itemset.detail_id
					}
				break;
			}
			ajax({
			  api: cmd,
			  method: 'GET',
			  data:params
			},(json) => {
				ajax({
				  api: 'updateItemsInRingDatabase',
				  method: 'GET',
				  data:params
				},(json) => {
					dispatch({
					  type: SHOW_LOADING,
					  loading:false
					});
					dispatch({type: ADD_ITEM_TO_ACTIVE,newItemSet:itemset});
				},(json) =>{
					const login = (json.code==210);
					dispatch({
						type: LOAD_ERR,
						message:json.msg+(json.sub_msg?json.sub_msg:""),
						loginFalse:login
					});
				});
			},(json) =>{
				const login = (json.code==210);
				dispatch({
					type: LOAD_ERR,
					message:json.msg+(json.sub_msg?json.sub_msg:""),
					loginFalse:login
				});
			});
			
		}
	}
}
export function getItemsIndepot(params) {
	return (dispatch) => {
		//发送请求前
		dispatch({
		  type: SHOW_LOADING,
		  loading:true
		});
		if(window.workbench){
			//千牛环境执行创建折扣,含有workbench参数为千牛环境
			  window.QN.top.invoke( {
					cmd : 'taobao.items.inventory.get',
					param : params,
					method:"post",
					success : function(rsp, cmd, param) {
						console.log(rsp,param);
					},
					error : function(msg, cmd, param) {
						console.log(msg,param);
					}
				} );
		}else{
			ajax({
			  api: 'getItemsIndepot',
			  method: 'GET',
			  data:params
			},(json) => {
				if(json.total_results>0){
					if(!(json.items.item instanceof Array)){
						json.items.item = Array(json.items.item);
					}
				}else{
					json.items={item:[]};
				}
				
				dispatch({
				  type: GET_ITEMS_IN_DEPOT,
				  items:json.items.item,
				  total_results:json.total_results,
				  page:params.page,
				  pagesize:params.pagesize,
				  cids:params.cids,
				  q:params.q
				});
			},(json) =>{
				const login = (json.code==210);
				dispatch({
					type: LOAD_ERR,
					message:json.msg+(json.sub_msg?json.sub_msg:""),
					loginFalse:login
				});
			});
			
		}
	}
}
export function getItemsOnSale(params) {
	return (dispatch) => {
		//发送请求前
		dispatch({
		  type: SHOW_LOADING,
		  loading:true
		});
		if(window.workbench){
			//千牛环境执行创建折扣,含有workbench参数为千牛环境
			  window.QN.top.invoke( {
					cmd : 'taobao.items.onsale.get',
					param : params,
					method:"post",
					success : function(rsp, cmd, param) {
						console.log(rsp,param);
					},
					error : function(msg, cmd, param) {
						console.log(msg,param);
					}
				} );
		}else{
			ajax({
			  api: 'getItemsOnSale',
			  method: 'GET',
			  data:params
			},(json) => {
				if(json.total_results>0){
					if(!(json.items.item instanceof Array)){
						json.items.item = Array(json.items.item);
					}
				}else{
					json.items={item:[]};
				}
				
				dispatch({
				  type: GET_ITEMS_ON_SEAL,
				  items:json.items.item,
				  total_results:json.total_results,
				  page:params.page,
				  pagesize:params.pagesize,
				  cids:params.cids,
				  q:params.q
				});
			},(json) =>{
				const login = (json.code==210);
				dispatch({
					type: LOAD_ERR,
					message:json.msg+(json.sub_msg?json.sub_msg:""),
					loginFalse:login
				});
			});
			
		}
	}
}
export function getItemsByIds(params) {
	return (dispatch) => {
		//发送请求前
		dispatch({
		  type: SHOW_LOADING,
		  loading:true
		});
		if(window.workbench){
			//千牛环境执行创建折扣,含有workbench参数为千牛环境
			  window.QN.top.invoke( {
					cmd : 'taobao.items.seller.list.get',
					param : {ids:params.ids},
					method:"post",
					success : function(rsp, cmd, param) {
						console.log(rsp,param);
					},
					error : function(msg, cmd, param) {
						console.log(msg,param);
					}
				} );
		}else{
			ajax({
			  api: 'getItemsByIds',
			  method: 'GET',
			  data:{ids:params.ids}
			},(json) => {
				if(json.items.item && !(json.items.item instanceof Array)){
					json.items.item = Array(json.items.item);
				}
				dispatch({
				  type: GET_ITEMS_ON_SEAL,
				  items:json.items.item,
				  total_results:params.total_results,
				  page:params.page,
				  pagesize:params.pagesize,
				  cids:params.cids,
				  q:params.q
				});
			},(json) =>{
				const login = (json.code==210);
				dispatch({
					type: LOAD_ERR,
					message:json.msg+(json.sub_msg?json.sub_msg:""),
					loginFalse:login
				});
			});
			
		}
	}
}
